import axios from 'axios';
import {message} from 'antd';

const instance = axios.create({
    baseURL: '/clpms',
    timeout: 50000,
});

// 校验是否登录，没有登录跳转到登录页面
instance.interceptors.response.use(
    (response) => {
        const {data} = response;
        if (data && data.code === 100004) {
            // 跳转到登录页面
            window.location.href = '/login';
            return Promise.reject(data);
        }
        return response;
    },
    (error) => {
        const {response} = error;

        if (response) {
            const {status, data} = response;
            if (status === 401 && data?.code === 100004) {
                // 跳转到登录页面
                window.location.href = '/login';
            }
        }
        message.error(error.message);
        return Promise.reject(error);
    }
);

export default instance;
